package com.yyt.libsrc.net;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.vondear.rxtool.RxAppTool;
import com.vondear.rxtool.RxTool;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import top.lixb.libcommon.base.BaseApplication;
import top.lixb.libcommon.log.L;


public class ParamsBuilder {

    protected TreeMap<String, String> mParams;

    public ParamsBuilder() {
        mParams = getSortMap();
    }

    public ParamsBuilder addP(String k, String v) {
        if (!TextUtils.isEmpty(v)) {
            mParams.put(k, v);
        }
        return this;
    }

    /**
     * 打印参数
     */
    protected void printParams(Map<String, String> mParams) {
        L.w("   \n\r");
        L.w("   \n\r");
        L.w("   \n\r");
        L.w("   \n\r");
        L.w("===============mParams===============");
        Set<Map.Entry<String, String>> entries = mParams.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String value = entry.getValue();
            if (TextUtils.isEmpty(value)) {
                value = "参数为空";
            }
            L.w(entry.getKey() + " : " + value);
        }
        L.w("===============mParams===============");
    }


    public Map build() {
        return addCommonParams();
    }

    public synchronized ParamsBuilder addCmd(String value) {
        mParams.put("_cmd", value);
        return this;
    }


    public synchronized ParamsBuilder addPage(int value) {
        mParams.put("page", String.valueOf(value));
        mParams.put("curPage", String.valueOf(value));
        mParams.put("pageSize", "10");
        return this;
    }

    @NonNull
    private TreeMap getSortMap() {
        return new TreeMap(new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                return obj1.compareTo(obj2);
            }
        });
    }

    /**
     * 添加公共参数
     */
    protected Map<String, String> addCommonParams() {//
        TreeMap params = getSortMap();
        params.put("code", Consts.Config.sign_code);
        params.put("ClientSystem", "Android");
        params.put("ClientVersion", String.valueOf(RxAppTool.getAppVersionCode(BaseApplication.getApp())));
        params.put("ApiVersion", "1.0");
        params.put("s_ticket", LocalHelper.getSTicket());
        params.put("member_type",LocalHelper.getMemberType());

        for (Map.Entry<String, String> stringStringEntry : mParams.entrySet()) {
            if (!TextUtils.isEmpty(stringStringEntry.getValue().trim())) {
                params.put(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }

        String urlParamsByMap = getUrlParamsByMap(params);//然后所有参数用&字符连接
        String sign = RxTool.Md5(urlParamsByMap);
        params.put("sign", sign.toLowerCase());
        printParams(params);
        return params;
    }

    protected String getUrlParamsByMap(Map<String, String> map) {
        if (map == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

}
